package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.LExebilldetail;
import com.ruoyi.system.mapper.LExebillMapper;
import com.ruoyi.system.domain.LExebill;
import com.ruoyi.system.service.ILExebillService;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;
import static com.ruoyi.system.utils.Code.getCode;
import static com.ruoyi.system.utils.Code.getNowDate;

/**
 * 派工单主Service业务层处理
 *
 * @author zyb
 * @date 2022-01-03
 */
@Service
public class LExebillServiceImpl extends ServiceImpl<LExebillMapper, LExebill> implements ILExebillService {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询派工单主
     *
     * @param billid 派工单主主键
     * @return 派工单主
     */
    @Override
    public LExebill selectLExebillByBillid(Long billid) {
        return baseMapper.selectLExebillByBillid(billid);
    }

    /**
     * 查询派工单主列表
     *
     * @param lExebill 派工单主
     * @return 派工单主
     */
    @Override
    public List<LExebill> selectLExebillList(LExebill lExebill) {
        return baseMapper.selectLExebillList(lExebill);
    }

    /**
     * 新增派工单主
     *
     * @param lExebill 派工单主
     * @return 结果
     */
    @Transactional
    @Override
    public int insertLExebill(LExebill lExebill) {
        if (!StringUtils.isNotBlank(lExebill.getBillcode())) {
            lExebill.setBillcode(String.valueOf(getBillCode()));
        }
        checkBillCode(lExebill);
        String str = getNowDate();
        int exebill = redisCache.getCacheObject("exebill" + str) == null ? 0 : redisCache.getCacheObject("exebill" + str);
        lExebill.setCreateBy(String.valueOf(getUserId()));
        lExebill.setCreateTime(DateUtils.getNowDate());
        int rows = baseMapper.insertLExebill(lExebill);
        exebill++;
        redisCache.setCacheObject("exebill" + str, exebill, 1, TimeUnit.DAYS);
        insertLExebilldetail(lExebill);
        return rows;
    }

    //验证订单编号是否重复
    public void checkBillCode(LExebill lExebill) {
        String Billcode = lExebill.getBillcode();
        LExebill param = new LExebill();
        param.setBillcode(Billcode);
        List<LExebill> exebills = selectLExebillList(param);
        if (exebills.size() > 0) {
            String str = getNowDate();
            String type = sysConfigService.selectConfigByKey("exe.bill.code.type");
            String billCode = type + "-" + str + "%";
            int num = baseMapper.selectCountByBillCode(billCode);
            redisCache.setCacheObject("exebill" + str, num, 1, TimeUnit.DAYS);
            lExebill.setBillcode(String.valueOf(getBillCode()));
            checkBillCode(lExebill, num);
        }


    }

    public void checkBillCode(LExebill lExebill, int num) {
        String Billcode = lExebill.getBillcode();
        LExebill param = new LExebill();
        param.setBillcode(Billcode);
        List<LExebill> exebills = selectLExebillList(param);
        if (exebills.size() > 0) {
            String str = getNowDate();
            num++;
            redisCache.setCacheObject("exebill" + str, num, 1, TimeUnit.DAYS);
            lExebill.setBillcode(String.valueOf(getBillCode()));
            checkBillCode(lExebill, num);
        }
    }

    /**
     * 修改派工单主
     *
     * @param lExebill 派工单主
     * @return 结果
     */
    @Transactional
    @Override
    public int updateLExebill(LExebill lExebill) {
        lExebill.setUpdateBy(String.valueOf(getUserId()));
        lExebill.setUpdateTime(DateUtils.getNowDate());
        baseMapper.deleteLExebilldetailByBillid(lExebill.getBillid());
        insertLExebilldetail(lExebill);
        return baseMapper.updateLExebill(lExebill);
    }

    /**
     * 批量删除派工单主
     *
     * @param billids 需要删除的派工单主主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLExebillByBillids(Long[] billids) {
        baseMapper.deleteLExebilldetailByBillids(billids);
        return baseMapper.deleteLExebillByBillids(billids);
    }

    /**
     * 删除派工单主信息
     *
     * @param billid 派工单主主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLExebillByBillid(Long billid) {
        baseMapper.deleteLExebilldetailByBillid(billid);
        return baseMapper.deleteLExebillByBillid(billid);
    }

    /**
     * 新增派工单明细信息
     *
     * @param lExebill 派工单主对象
     */
    public void insertLExebilldetail(LExebill lExebill) {
        List<LExebilldetail> lExebilldetailList = lExebill.getLExebilldetailList();
        Long billid = lExebill.getBillid();
        if (StringUtils.isNotNull(lExebilldetailList)) {
            List<LExebilldetail> list = new ArrayList<LExebilldetail>();
            for (LExebilldetail lExebilldetail : lExebilldetailList) {
                lExebilldetail.setBillid(billid);
                lExebilldetail.setCreateBy(String.valueOf(getUserId()));
                lExebilldetail.setCreateTime(DateUtils.getNowDate());
                list.add(lExebilldetail);
            }
            if (list.size() > 0) {
                baseMapper.batchLExebilldetail(list);
            }
        }
    }

    @Override
    public Object getBillCode() {
        String str = getNowDate();
        String type = sysConfigService.selectConfigByKey("exe.bill.code.type");
        String billCode = type + "-" + str + "-";
        int exebill = redisCache.getCacheObject("exebill" + str) == null ? 0 : redisCache.getCacheObject("exebill" + str);
        exebill++;
        billCode = getCode(billCode, exebill);
        return billCode;
    }
}
